<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/4/3 14:42
 * @version
 */
namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\News;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class NewsController extends Controller
{
    public function search(Request $request)
    {
        return view('main::news',$request->all());
    }

    public function apiSearch(Request $request)
    {
        $data = [];
        $data['total'] = News::query()->count();
        $data['items'] = News::query()->with('image')->get();
        return success($data);
    }

    public function apiAdd(Request $request)
    {
        $input = $request->only(['title','detail','image_id']);
        $input['sort'] = $request->input('sort',0);
        $input['created_at'] = Carbon::now();
        $cid = News::query()->insertGetId($input);
        if($cid) return success('添加成功');
    }

    public function apiUpdate(Request $request)
    {
        $cid = $request->input('id');
        $input = $request->only(['title','detail','image_id']);
        $input['sort'] = $request->input('sort',0);
        if($cid) {
            News::query()->where('id',$cid)->update($input);
        }
        return success('更新成功');
    }

    public function apiDelete(Request $request)
    {
        $id = $request->input('id');
        if($id){
            News::query()->where('id',$id)->delete();
        }
        return success();
    }
}